package web.beans;

import Weather.Forecast;
import entity.User.bo.UserBo;
import entity.User.model.User;
import util.HttpRequests;

import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.validator.ValidatorException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Date;
import java.util.ArrayList;
import java.util.Locale;

/**
 * Created by vanches on 3.1.15.
 */
public class UserBean implements Serializable {
    private int id;
    private String login;
    private int accessLevel;
    private String email;
    private boolean moderator;
    private String city;
    private String locale;
    private String avatar;
    private Date birhday;
    private UserBo userBo;
    private ArrayList<String> listLocale=new ArrayList<String>();
    private Forecast forecast;
    private final String  yandexWather="http://export.yandex.ru/weather-ng/forecasts/";


    public void setInfo(User user) {
        id = user.getId();
        login = user.getLogin();
        accessLevel = user.getAccessLevel();
        email = user.getMail();
        locale = user.getLocale();

        avatar = user.getAvatar();
        city=user.getCityId();
        birhday=user.getBirthday();
        if (accessLevel > 1) {
            moderator = true;
        } else {
            moderator = false;
        }




    }
    public String quit() {
        id = 0;
        login = null;
        accessLevel = 0;
        email = null;
        locale ="ru";
        avatar = null;
        city=null;
        forecast=null;
        birhday=null;

        return "quest";
    }
    public void updateWeather(){
        downloaderWeather();
    }
    private void downloaderWeather(){
        String xml=null;
        HttpRequests  httpRequests=new HttpRequests();
        try {
            xml=httpRequests.sendGet(city,yandexWather);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        InputStream pStm = new ByteArrayInputStream(xml.getBytes());
        try {
            JAXBContext  jaxbContext = JAXBContext.newInstance(Forecast.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            forecast = (Forecast) jaxbUnmarshaller.unmarshal(pStm);
        } catch (JAXBException e1) {
            e1.printStackTrace();
        }
    }



    //region Setting
    public void updateUser() {
        User newUser = userBo.getUserById(id);
        newUser.setLogin(login);
        newUser.setAccessLevel(accessLevel);
        newUser.setMail(email);
        newUser.setCityId(city);
        newUser.setLocale(locale);
        newUser.setBirthday(birhday);
        userBo.update(newUser);

    }
    public void citiesSet(ValueChangeEvent e) {
        if(e.getNewValue()!=null && !e.getNewValue().toString().equals(""))
            city = e.getNewValue().toString();
    }
    public void localeSet(ValueChangeEvent e) {
        if(e.getNewValue()!=null && !e.getNewValue().toString().equals(""))
        locale = e.getNewValue().toString();
    }
    public void checkLogin(FacesContext context,
                           UIComponent toValidate,
                           Object value) throws ValidatorException {
        String login = (String) value;

        if (userBo.checkLogin(login) && !login.equals(this.login)) {
            FacesMessage message = new FacesMessage("Логин занят!" );
            throw new ValidatorException(message);
        }
    }



        //endregion

    //region GET and SET

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public int getAccessLevel() {
        return accessLevel;
    }

    public void setAccessLevel(int accessLevel) {
        this.accessLevel = accessLevel;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public boolean isModerator() {
        return moderator;
    }


    public String getLocale() {
        return locale;
    }

    public void setLocale(String locale) {
        if(locale!=null && !locale.equals("")) {
            this.locale = locale;
        }
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public UserBo getUserBo() {
        return userBo;
    }

    public void setUserBo(UserBo userBo) {
        this.userBo = userBo;
    }

    public ArrayList<String> getListLocale() {
        if(listLocale.size()==0) {
            listLocale.add("ru");
            listLocale.add("en");
        }
        return listLocale;
    }

    public void setListLocale(ArrayList<String> listLocale) {
        this.listLocale = listLocale;
    }

    public Forecast getForecast() {
        return forecast;
    }

    public Date getBirhday() {
        java.sql.Date sqlDate = new java.sql.Date(birhday.getTime());
        return sqlDate;
    }

    public void setBirhday(Date birhday) {
        this.birhday = birhday;
    }

    //endregion
}
